#include "StdAfx.h"
#include "RagexePatcher.h"

RagexePatcher::RagexePatcher(void)
{
	iniPath = new _TCHAR[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, iniPath); 
	_tcscat_s(iniPath, MAX_PATH, TEXT("\\cro_patcher.ini"));
	unlimitSight=GetPrivateProfileInt(TEXT("ragexe"),TEXT("unlimitSight"),0,iniPath);
	hallucination=GetPrivateProfileInt(TEXT("ragexe"),TEXT("hallucination"),0,iniPath);
	hairstyle=GetPrivateProfileInt(TEXT("ragexe"),TEXT("hairstyle"),0,iniPath);
	unlimitTalk=GetPrivateProfileInt(TEXT("ragexe"),TEXT("unlimitTalk"),0,iniPath);
	removeShake=GetPrivateProfileInt(TEXT("ragexe"),TEXT("removeShake"),0,iniPath);
	removeDark=GetPrivateProfileInt(TEXT("ragexe"),TEXT("removeDark"),0,iniPath);
}

RagexePatcher::~RagexePatcher(void)
{
}

void RagexePatcher::Patch(HANDLE hProcess)
{
	if (unlimitSight)
	{
		ExcutePatch(hProcess, (LPVOID)0x008C8546, 0x00);
		ExcutePatch(hProcess, (LPVOID)0x008C854A, 0xB5);
		ExcutePatch(hProcess, (LPVOID)0x008C854B, 0x44);

		ExcutePatch(hProcess, (LPVOID)0x0071AE6D, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x0071AE6E, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x0071AE6F, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x0071AE70, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x0071AE71, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x0071AE72, 0x90);
	}
	if (hallucination)
	{
		ExcutePatch(hProcess, (LPVOID)0x00422261, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x00422262, 0xE9);
	}
	if (hairstyle)
	{
		ExcutePatch(hProcess, (LPVOID)0x0047B256, 0x1A);
		ExcutePatch(hProcess, (LPVOID)0x00483276, 0x1B);
		ExcutePatch(hProcess, (LPVOID)0x0048327A, 0x1A);
	}
	if (unlimitTalk)
	{
		ExcutePatch(hProcess, (LPVOID)0x00721736, 0x64);
	}
	if (removeShake)
	{
		ExcutePatch(hProcess, (LPVOID)0x00571B00, 0xC2);
		ExcutePatch(hProcess, (LPVOID)0x00571B01, 0x0C);
		ExcutePatch(hProcess, (LPVOID)0x00571B02, 0x00);
		ExcutePatch(hProcess, (LPVOID)0x00571B03, 0x90);
		ExcutePatch(hProcess, (LPVOID)0x00571B20, 0xC2);
		ExcutePatch(hProcess, (LPVOID)0x00571B21, 0x14);
		ExcutePatch(hProcess, (LPVOID)0x00571B22, 0x00);
		ExcutePatch(hProcess, (LPVOID)0x00571B23, 0x90);
	}
	if (removeDark)
	{
		ExcutePatch(hProcess, (LPVOID)0x00695CF3, 0x6B);
		ExcutePatch(hProcess, (LPVOID)0x00695CD5, 0x3F);
	}
}

void RagexePatcher::Save()
{
	WritePrivateProfileString(TEXT("ragexe"),TEXT("unlimitSight"),unlimitSight?TEXT("1"):TEXT("0"),iniPath);
	WritePrivateProfileString(TEXT("ragexe"),TEXT("hallucination"),hallucination?TEXT("1"):TEXT("0"),iniPath);
	WritePrivateProfileString(TEXT("ragexe"),TEXT("hairstyle"),hairstyle?TEXT("1"):TEXT("0"),iniPath);
	WritePrivateProfileString(TEXT("ragexe"),TEXT("unlimitTalk"),unlimitTalk?TEXT("1"):TEXT("0"),iniPath);
	WritePrivateProfileString(TEXT("ragexe"),TEXT("removeShake"),removeShake?TEXT("1"):TEXT("0"),iniPath);
	WritePrivateProfileString(TEXT("ragexe"),TEXT("removeDark"),removeDark?TEXT("1"):TEXT("0"),iniPath);
}

void RagexePatcher::ExcutePatch(HANDLE hProcess, LPVOID lpAddr, int value)
{
	DWORD dwNumberOfBytesRead;
	//DWORD oldProtect;

	//VirtualProtectEx(hProcess, lpAddr, 1, PAGE_READWRITE, &oldProtect);
	WriteProcessMemory(hProcess, lpAddr, &value, 1, &dwNumberOfBytesRead);
	//VirtualProtectEx(hProcess, lpAddr, 1, oldProtect, NULL);
}